unit IBERestMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, ExtCtrls;

type
  TIBRestMainForm = class(TForm)
    OldDBName: TEdit;
    NewDBName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    TableBox: TCheckBox;
    SPBox: TCheckBox;
    ReportBox: TCheckBox;
    RunBtn: TButton;
    TrigBox: TCheckBox;
    ExitBtn: TButton;
    DomainBox: TCheckBox;
    IndexBox: TCheckBox;
    ExpBox: TCheckBox;
    GenBox: TCheckBox;
    ProgBar: TProgressBar;
    Label3: TLabel;
    ScriptBox: TRadioButton;
    RepairBox: TRadioButton;
    Bevel1: TBevel;
    UDFBox: TCheckBox;
    AboutBtn: TButton;
    EditDialect: TComboBox;
    procedure RunBtnClick(Sender: TObject);
    procedure ExitBtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure AboutBtnClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    // parameters for batch
  protected
    procedure CheckDmn; virtual;
    procedure CheckExp; virtual;
    procedure CheckGen; virtual;
    procedure CheckTbl; virtual;
    procedure CheckIdx; virtual;
    procedure CheckSP; virtual;
    procedure CheckTrig; virtual;
    procedure CheckUDF; virtual;
    procedure CheckDataBase; virtual;
  public
    { Public declarations }
    Password : String;
    FOldDB,
    FNewDB : String;
    FAutoRun,
    FDmn,
    FExp,
    FGen,
    FTbl,
    FIdx,
    FSP,
    FTrig,
    FUDF : Boolean;
  end;

var
  IBRestMainForm: TIBRestMainForm;

implementation

uses Data, DomainRest, ExpRest, GenRest, TableRest, IdxRest, SPRest, TrigRest, UDFRest,
     AboutIB, ReportForm, IBRestLogin, DropRest;

{$R *.DFM}

procedure TIBRestMainForm.FormCreate(Sender: TObject);
var i : Integer;
begin
  FOldDB:='';
  FNewDB:='';
  Password:='';
  DM:=TDM.Create(Self);
  SP:=TSP.Create(Self);
  Trig:=TTrig.Create(Self);
  Tbl:=TTbl.Create(Self);
  Idx:=TIdx.Create(Self);
  Gen:=TGen.Create(Self);
  Rep:=TRep.Create(Self);
  Dmn:=TDmn.Create(Self);
  Exp:=TExp.Create(Self);
  UDF:=TUDF.Create(Self);
  Drop:=TDrop.Create(Self);
  AboutIBForm:=TAboutIBForm.Create(Self);

  // are there any parameters ?
  FAutoRun:=False;
  FDmn:=False;
  FExp:=False;
  FGen:=False;
  FTbl:=False;
  FIdx:=False;
  FSP:=False;
  FTrig:=False;
  FUDF:=False;
  for i:=1 to ParamCount do
  begin
    if UpperCase(Copy(ParamStr(i),1,4))='/RUN' then FAutoRun:=True else
      if UpperCase(Copy(ParamStr(i),1,4))='/DMN' then FDmn:=True else
    if UpperCase(Copy(ParamStr(i),1,4))='/EXP' then FExp:=True else
      if UpperCase(Copy(ParamStr(i),1,4))='/GEN' then FGen:=True else
    if UpperCase(Copy(ParamStr(i),1,4))='/TBL' then FTbl:=True else
      if UpperCase(Copy(ParamStr(i),1,4))='/IDX' then FIdx:=True else
    if UpperCase(Copy(ParamStr(i),1,3))='/SP' then FSP:=True else
      if UpperCase(Copy(ParamStr(i),1,5))='/TRIG' then FTrig:=True else
    if UpperCase(Copy(ParamStr(i),1,4))='/UDF' then FUDF:=True else
      if UpperCase(Copy(ParamStr(i),1,3))='/D:' then FOldDB:=Copy(ParamStr(i),4,Length(ParamStr(i))-3) else
    if UpperCase(Copy(ParamStr(i),1,2))='/3' then DM.Dialect:=3 else
      if UpperCase(Copy(ParamStr(i),1,3))='/T:' then FNewDB:=Copy(ParamStr(i),4,Length(ParamStr(i))-3);
  end;
end;

procedure TIBRestMainForm.FormDestroy(Sender: TObject);
begin
  AboutIBForm.Free;
  Drop.Free;
  UDF.Free;
  Exp.Free;
  Dmn.Free;
  Rep.Free;
  Gen.Free;
  Idx.Free;
  Tbl.Free;
  Trig.Free;
  SP.Free;
  DM.Free;
end;

procedure TIBRestMainForm.FormShow(Sender: TObject);
begin
  if FAutoRun then
  begin
    DomainBox.Checked:=FDmn;
    ExpBox.Checked:=FExp;
    GenBox.Checked:=FGen;
    UDFBox.Checked:=FUDF;
    TableBox.Checked:=FTbl;
    IndexBox.Checked:=FIdx;
    SPBox.Checked:=FSP;
    TrigBox.Checked:=FTrig;
    RepairBox.Checked:=True;
    ScriptBox.Checked:=False;
    ReportBox.Checked:=False;
  end;
  if FOldDB<>'' then OldDBName.Text:=FOldDB;
  if FNewDB<>'' then NewDBName.Text:=FNewDB;
  case DM.Dialect of
    1 : EditDialect.ItemIndex:=0;
    3 : EditDialect.ItemIndex:=1;
  end;
end;

procedure TIBRestMainForm.FormActivate(Sender: TObject);
begin
  if FAutoRun then
  begin
    RunBtnClick(Sender);
    Close;
  end;
end;

procedure TIBRestMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=VK_F12 then
    Rep.ShowModal;
end;

procedure TIBRestMainForm.CheckDmn;
begin
  Label3.Caption:='Domains';
  Application.ProcessMessages;
  CheckDomains(ProgBar);
end;

procedure TIBRestMainForm.CheckExp;
begin
  Label3.Caption:='Exceptions';
  Application.ProcessMessages;
  CheckExceptions(ProgBar);
end;

procedure TIBRestMainForm.CheckGen;
begin
  Label3.Caption:='Generators';
  Application.ProcessMessages;
  CheckGenerators(ProgBar);
end;

procedure TIBRestMainForm.CheckTbl;
begin
  Label3.Caption:='Table Fields';
  Application.ProcessMessages;
  CheckTables(ProgBar);
end;

procedure TIBRestMainForm.CheckIdx;
begin
  Label3.Caption:='Primary keys';
  Application.ProcessMessages;
  CheckPrimaryKeys(ProgBar);
  Label3.Caption:='Secondary indices';
  Application.ProcessMessages;
  CheckSecondaryIndices(ProgBar);
  Label3.Caption:='Foreign keys';
  Application.ProcessMessages;
  CheckForeignKeys(ProgBar);
end;

procedure TIBRestMainForm.CheckSP;
begin
  Label3.Caption:='Stored Procedures';
  Application.ProcessMessages;
  CheckStoredProcedures(ProgBar);
end;

procedure TIBRestMainForm.CheckTrig;
begin
  Label3.Caption:='Triggers';
  Application.ProcessMessages;
  CheckTriggers(ProgBar);
end;

procedure TIBRestMainForm.CheckUDF;
begin
  Label3.Caption:='UDF';
  Application.ProcessMessages;
  CheckUDFs(ProgBar);
end;

procedure TIBRestMainForm.CheckDataBase;
begin
  if DomainBox.Checked then CheckDmn;
  if ExpBox.Checked then CheckExp;
  if GenBox.Checked then CheckGen;
  if UDFBox.Checked then CheckUDF;
  if TableBox.Checked then CheckTbl;
  if IndexBox.Checked then CheckIdx;
  if SPBox.Checked then CheckSP;
  if TrigBox.Checked then CheckTrig;
  Label3.Caption:='';
  ProgBar.Position:=0;
end;

procedure TIBRestMainForm.RunBtnClick(Sender: TObject);
var UserName : String;
begin
  UserName:='';
  Rep.InitReport;
  DM.AutoRepair:=RepairBox.Checked;
  DM.ShowReport:=ReportBox.Checked;
  case EditDialect.ItemIndex of
    0 : DM.Dialect:=1;
    1 : DM.Dialect:=3;
  end;
  DM.OldDB.Params.Values['Database']:=OldDBName.Text;
  try
    if Password='' then
    begin
      IBRestLoginForm:=TIBRestLoginForm.Create(Self);
      if IBRestLoginForm.ShowModal=mrOk then
      begin
        Password:=IBRestLoginForm.EditPass.Text;
        UserName:=IBRestLoginForm.EditUser.Text;
        IBRestLoginForm.Free;
      end;
    end;
    if Password<>'' then DM.OldDB.Params.Values['Password']:=Password;
    if UserName<>'' then DM.OldDB.Params.Values['user_name']:=UserName;
    DM.ConnectOldDB;
    DM.NewDB.Params.Values['Database']:=NewDBName.Text;
    try
      if Password<>'' then DM.NewDB.Params.Values['Password']:=Password;
      if UserName<>'' then DM.NewDB.Params.Values['user_name']:=UserName;
      DM.ConnectNewDB;

      CheckDataBase;
      if not DM.AutoRepair then
        DM.S.SaveToFile(ExtractFilePath(ParamStr(0))+'ibrest.sql');
      if ReportBox.Checked then
      begin
        Rep.btRepair.Visible:=not DM.AutoRepair;
        Rep.btScript.Visible:=not DM.AutoRepair;
        if Rep.ShowModal=mrOk then
        begin
          DM.AutoRepair:=True;
          DM.ShowReport:=False;
          CheckDataBase;
        end;
      end;
    finally
      DM.NewDB.Close;
    end;
  finally
    DM.OldDB.Close;
  end;
end;

procedure TIBRestMainForm.ExitBtnClick(Sender: TObject);
begin
  Close;
end;

procedure TIBRestMainForm.AboutBtnClick(Sender: TObject);
begin
  AboutIBForm.ShowModal;
end;

end.
